<template>
  <div class="changePos">
    <div class="header">
      <h1>部门管理</h1>
      <div class="header-right">
        <i class="el-icon-s-home"></i>
        <span class="font-left">首页</span>
        <i class="el-icon-arrow-right"></i>
        <span class="font-right">部门管理</span>
      </div>
    </div>
    <div class="content">
      <div class="bar">
        <el-button
          type="info"
          icon="el-icon-arrow-left"
          size="mini"
          @click="goBack"
        >
          返回
        </el-button>
      </div>
      <el-form
        size="small"
        ref="changeForm"
        :model="changeForm"
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="title">
              <span slot="label">
                <span class="labelText">名称</span>
              </span>
              <el-input v-model="changeForm.title" :validate-event="false"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="level">
              <span slot="label">
                <span class="labelText">层级</span>
              </span>
              <el-input v-model="changeForm.level" :validate-event="false"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item class="btnBox">
          <el-button type="primary" size="small" @click="($event) => changePos('changeForm')"
            >保存</el-button
          >
          <el-button type="info" size="small" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  data() {
    return {
      changeForm:{
        title:'',
        level:''
      },
      rules: {
        title: [
          { required: true, message:"请输入名称", trigger: "blur" },
        ],
        level:[
          { required: true, message:"请输入名称", trigger: "blur" },
        ]
      },
    };
  },

  mounted() {
    this.changeForm = this.$route.params.row
  },
  created() {},


  methods: {
    //获取数据 把数据放在input框里
    
    goBack() {
      this.$router.push("/user/posManage");
    },
    changePos(changeForm) {
      this.$refs[changeForm].validate((valid) => {
        if (valid){
          this.changeForm.workTime = new Date(
          dayjs().format("YYYY-MM-DD HH:mm:ss")
        ).toISOString();
          this.$api.user.updatePos(this.changeForm).then((res) => {
          if (res.data.code === 200) {
            //跳转到列表页面
            this.$router.push("/user/posManage");
            console.log(res);
            //提示
            this.$message({
              message: "添加部门成功",
              type: "success",
            });
          } else {
            //错误信息提示
            this.$message.error("出现错误，添加部门失败");
            console.log(res);
          }
        });
        }else{
          this.$message.error( '请填写必填项')
        }
        
      });
    },
    //取消按钮
    cancel(){
      this.$router.push("/user/posManage")
    }
  },
};
</script>

<style lang="less" scoped>
html body {
  height: 100%;
  background-color: #f5f5f5;
}
.addDep {
  height: 100%;
}
.header {
  position: relative;
  padding: 15px 15px 0 15px;
  display: flex;
  justify-content: space-between;
}
.header > h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}
.header-right {
  font-size: 12px;
  display: flex;
  align-items: center;
}
.el-icon-arrow-right {
  padding: 0 5px;
  color: #ccc;
}
.el-icon-s-home {
  color: #444;
  display: inline-block;
  margin-right: 5px;
}
.font-left {
  color: #444;
}
.font-right {
  color: #777;
}
.content {
  padding: 15px;
}
.bar {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  border-top: 3px solid #d2d6de;
  border-bottom: 1px solid #f4f4f4;
}
.labelText {
  color: #333333;
  font-size: 14px;
}
.labelText2 {
  color: #999999;
  font-size: 12px;
  margin-left: 5px;
}
.addForm {
  background-color: #fff;
  padding: 5px;
}
</style>
